#!/bin/bash
systemctl disable --now firewalld NetworkManager
sed -ri "s/^SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config

# machines's swap partment offline.
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab

Manu () {
cat <<EOF
-----------------------------------------
|       1.安装阿里云基础Base仓库        |
|       2.安装阿里云dockers仓库         |
|       3.安装阿里云epel仓库            |
|       4.安装docker-compose            |
|       5.                              |
|       6.退出脚本                      |
-----------------------------------------
EOF
}

next () {
read -p "请输入继续安装还是停止[y/n]:" st
if [ $st = y ];then
clear manu
echo "请输入你要安装的仓库[1..6]: "
elif [ $st = n ];then
break
fi
}

space () {
echo "-------------------------------------------------------------------------------------"
echo ""
echo ""
}

while :
do

Manu
read -p "请输入你要安装的yum仓库:" install
if [ $install = 1 ] ;then
echo "安装阿里云Base.repo"
#安装 wget curl vim 工具
yum install -y wget curl vim
#移出原有yum源
mv /etc/yum.repos.d/*.repo /tmp/
#获取阿里base-repo
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#刷新缓存
yum makecache fast
#检查是否替换成功
cat /etc/yum.repos.d/CentOS-Base.repo | grep aliyun
space
next

elif [ $install = 2 ] ;then
echo "开始安装阿里云dockers仓库"
# step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce
# Step 4: 开启Docker服务
service docker start && systemctl status docker
# step 5: 配置docker镜像加速器服务
docker_daemon=/etc/docker/daemon.json
cat $docker_daemon
if [ $? -ne 0 ] ;then
touch $docker_daemon
cat $docker_daemon  <<EOF
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
EOF

else echo "{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}"  >> $docker_daemon
fi
space
next

elif [ $install = 3 ] ;then
echo "开始安装阿里云epel仓库"
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
#刷新缓存
yum makecache fast
#检查是否替换成功
cat /etc/yum.repos.d/epel.repo | grep aliyun
space
next

elif [ $install = 4 ] ;then
echo "开始安装docker-compose"
curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
space
next
elif [ $install = 6 ] ;then
echo "退出脚本"
exit
fi

done
